package com.jintoufs.web.action.message;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.domain.constants.MessageConstants;
import com.jintoufs.domain.message.Message;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.message.MessageService;
import com.jintoufs.web.action.support.JqgridPage;
import com.jintoufs.web.action.support.SupportAction;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yh
 * 2017-04-28
 */
@Controller
@Scope("prototype")
@RequestMapping("/message")
public class MessageAction extends SupportAction {
    @Resource
    private MessageService messageService;


    @RequestMapping("/list")
    public String list() {
        return "message/list";
    }

    @RequestMapping("/oper")
    @SystemControllerLog(description = "操作通知")
    @ResponseBody
    public AjaxMsg oper(String[] id, String oper, Message message) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        ajaxMsg.setCode(AjaxMsg.SUCCESS);
        try {
            if (oper.equals("del") && id != null) {
                for (int i = 0; i < id.length; i++) {
                    messageService.delete(Integer.parseInt(id[i]));
                }
            } else {
                if (oper.equals("edit")) {
                    messageService.update(message);
                } else if (oper.equals("add")) {
                    message.setCreateTime(new Date());
                    messageService.insert(message);
                } else {
                    ajaxMsg.setCode(AjaxMsg.FAILURE);
                    ajaxMsg.setMessage(MessageConstants.OPER_ERROR_MESSAGE);
                }
            }
        } catch (Exception e) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage(MessageConstants.SYSTEM_ERROR_MESSAGE);
        }
        return ajaxMsg;
    }


    @RequestMapping("/delete")
    @SystemControllerLog(description = "删除通知")
    public void delete(HttpServletResponse response, Integer id) {
        messageService.delete(id);
        writeInfo(response, JSON.toJSONString(new AjaxMsg()));
    }

    /**
     * input.do?id= 进入类目编辑页面 <br>
     * input.do 进入类目新增页面（不包含选中父类目）
     *
     * @param id    类目Id
     * @param model
     * @return vm/category/input.vm
     */
    @RequestMapping("input")
    public String input(Integer id, Model model) {
        Map<String, Object> result = Maps.newHashMap();
        String title;

        // 是否为编辑保存
        String isEdit;

        // 修改
        if (id != null && id != 0) {
            title = "修改通知";
            result = messageService.queryById(id);

            isEdit = "edit";
        } else {
            title = "新增通知";
            isEdit = "add";
        }

        model.addAttribute("isEdit", isEdit);
        model.addAttribute("result", result);
        model.addAttribute("title", title);


        return "message/input";
    }

    @RequestMapping("/query")
    @ResponseBody
    public JqgridPage query(int page, int rows, String sidx, String sord, String filters) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_ = null;
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        List<Map<String,Object>> list = messageService.getByFilter(filters_);
        // 用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(), pageInfo.getList());
    }

    /**
     * 获取详细信息
     */
    @RequestMapping("/getDetail")
    public String getDetail(Integer id, Model model) {
        Map<String, Object> result = new HashMap<String, Object>();
        if (id != null && id != 0) {
            result = messageService.queryById(id);
        }
        model.addAttribute("result", result);
        return "message/input";
    }

}
